package org.elanfox.intellicommunity.biz.service.core.msg.impl;

import org.elanfox.intellicommunity.biz.dto.msg.AlarmOptLogSaveDTO;
import org.elanfox.intellicommunity.biz.entity.auth.SystemManager;
import org.elanfox.intellicommunity.biz.entity.msg.AlarmMsgOptLogPO;
import org.elanfox.intellicommunity.biz.repository.msg.AlarmMsgOptLogRepository;
import org.elanfox.intellicommunity.biz.service.core.msg.AlarmMsgOptLogService;
import org.elanfox.intellicommunity.common.ienum.MsgEnums;
import org.elanfox.intellicommunity.data.base.base.AbstractCrudService;
import org.elanfox.intellicommunity.data.base.dto.BaseSaveDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2019/12/27
 */
@Service
public class AlarmMsgOptLogServiceImpl extends AbstractCrudService<AlarmMsgOptLogPO, Long> implements AlarmMsgOptLogService {
    private AlarmMsgOptLogRepository alarmMsgOptLogRepository;

    @Autowired
    protected AlarmMsgOptLogServiceImpl(AlarmMsgOptLogRepository repository) {
        super(repository);
        this.alarmMsgOptLogRepository = repository;
    }

    @Override
    public List<AlarmMsgOptLogPO> findByGroup(Long groupId) {
        return alarmMsgOptLogRepository.findByAlarmMsgGroupPO_GroupId(groupId);
    }

    @Override
    public AlarmMsgOptLogPO findByGroupAndStatus(Long groupId, MsgEnums.AlarmStatus alarmStatus) {
        return alarmMsgOptLogRepository.findByAlarmMsgGroupPO_GroupIdAndAlarmStatus(groupId, alarmStatus);
    }

    @Override
    @Transactional
    public AlarmMsgOptLogPO save(BaseSaveDTO saveDTO) {
        AlarmOptLogSaveDTO logSaveDTO = (AlarmOptLogSaveDTO) saveDTO;

        AlarmMsgOptLogPO alarmMsgOptLogPO = new AlarmMsgOptLogPO();
        alarmMsgOptLogPO.setAlarmStatus(logSaveDTO.getAlarmStatus());
        alarmMsgOptLogPO.setAlarmMsgGroupPO(logSaveDTO.getAlarmMsgGroupPO());
        SystemManager manager = (SystemManager) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        alarmMsgOptLogPO.setOptId(manager.getUserId());
        alarmMsgOptLogPO.setOptName(manager.getLoginName());
        alarmMsgOptLogPO.setPoliceName(logSaveDTO.getPoliceName());
        alarmMsgOptLogPO.setPoliceMobile(logSaveDTO.getPoliceMobile());
        alarmMsgOptLogPO.setRemark(logSaveDTO.getRemark());

        return repository.save(alarmMsgOptLogPO);
    }
}
